﻿using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Lamex.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Lamex.Library.CommonClass
{
    public class LanguageObject
    {
        private static GridView _View = null;
        private static string _XMLPath;
        private static string _GridFullName;
        private static XmlDocument _XMLDoc = new XmlDocument();
        public static void RegisterLanguage(GridView view)
        {
            _View = view;
            ReadGridConfig(); //读取之前保存的信息.
        }
        //从XML文件获取表格配置信息.
        public static void ReadGridConfig()
        {
            string path = GetConfigPath();
            if (!File.Exists(path))
                File.Create(path);
            GridConfig(_View);
            if (!File.Exists(_XMLPath)) CreateXML();
            _XMLDoc.Load(_XMLPath); //加载XML文件.
            string srh = string.Format("Root/Grid[@Name='{0}']", _GridFullName);
            XmlNode node = _XMLDoc.SelectSingleNode(srh);
            if (null == node)
                CreateNewGridConfig();
            else
            {
                GetGridConfigFromXML(node);
            }
            _View.BestFitColumns();
        }
        private static string GetConfigPath()
        {
            string languatePath = "";
            if (Loginer.CurrentUser.ChangeLanguage == 0)
                languatePath = Application.StartupPath + @"\zh-Hans\ChsGrid.xml";
            else
                languatePath = Application.StartupPath + @"\en-us\EngGrid.xml";
            return languatePath;
        }
        private static void CreateXML()
        {
            try
            {
                XmlDeclaration title = _XMLDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
                XmlElement root = _XMLDoc.CreateElement("Root");
                _XMLDoc.AppendChild(title);
                _XMLDoc.AppendChild(root);
                _XMLDoc.Save(_XMLPath);
            }
            catch { }
        }
        private static void CreateNewGridConfig()
        {
            try
            {
                XmlElement grid;
                XmlElement column;

                XmlNode root = _XMLDoc.SelectSingleNode("Root");
                grid = _XMLDoc.CreateElement("Grid");
                grid.SetAttribute("Name", _GridFullName);
                root.AppendChild(grid);
                foreach (GridColumn col in _View.Columns)
                {
                    column = _XMLDoc.CreateElement("Column");
                    SetColumnConfig(col, column);
                    grid.AppendChild(column);
                }
                _XMLDoc.Save(_XMLPath);
            }
            catch { }
        }
        private static void GetGridConfigFromXML(XmlNode gridNode)
        {
            string colName;
            _View.GridControl.BeginInit();
            foreach (XmlElement ele in gridNode.ChildNodes)
            {
                colName = ele.GetAttribute("Name");
                GridColumn column = GetColumnByName(colName);
                if (column != null) //column found
                {
                    column.Caption = ele.GetAttribute("Caption");
                    column.Width = int.Parse(ele.GetAttribute("Width"));
                    column.Visible = bool.Parse(ele.GetAttribute("Visible"));
                    column.VisibleIndex = int.Parse(ele.GetAttribute("VisibleIndex"));
                }
            }
            _View.GridControl.EndInit();
        }
        private static GridColumn GetColumnByName(string colName)
        {
            GridColumn col = null;
            foreach (GridColumn c in _View.Columns)
                if (c.Name == colName) return c;
            return col;
        }
        private static void SetColumnConfig(GridColumn col, XmlElement columnNode)
        {
            columnNode.SetAttribute("Name", col.Name);
            columnNode.SetAttribute("Caption", col.Caption);
            columnNode.SetAttribute("Width", col.Width.ToString());
            columnNode.SetAttribute("Visible", col.Visible.ToString());
            columnNode.SetAttribute("VisibleIndex", col.VisibleIndex.ToString());
        }

        public static void  GridConfig(GridView view)
        {
            _View = view;
            _GridFullName = view.GridControl.FindForm().GetType().FullName + @"\" + view.Name;
            _XMLPath = GetConfigPath();
        }
    }
}
